package main

import (
	"io/ioutil"
	"fmt"
	"encoding/json"
)

type GrafanaConf struct {
	Templating		Template 	    `json:"templating" form:"templating"`
	Panels		    []Panel			`json:"panels" form:"panels"`
	Title      		 string 		`json:"title" form:"title"`
}

type Template struct {
	List		[]Variable 					`json:"list" form:"list"`
}

type Variable struct {
	Name        string 		`json:"name" form:"name"`
	Label       string 		`json:"label" form:"label"`
	Regex       string 		`json:"regex" form:"regex"`
	Type        string 		`json:"type" form:"type"`
}

type Panel struct {
	Type        string 		`json:"type" form:"type"`
	Title       string 		`json:"title" form:"title"`
	Targets     []Target 	`json:"targets" form:"targets"`
	Options		Options		`json:"options" form:"options"`
}

type Target struct {
	Expr         string 		`json:"expr" form:"expr"`
	LegendFormat string 		`json:"legendFormat" form:"legendFormat"`
}

type Options struct {
	FieldOptions    FieldOptions	`json:"fieldOptions" form:"fieldOptions"`
}

type FieldOptions struct {
	Calcs         []string 		`json:"calcs" form:"calcs"`
}


func main(){
	bytes, err := ioutil.ReadFile("./test-1653443924584.json")
	if err != nil {
		fmt.Println("读取json⽂件失败", err)
		return
	}
	u := &GrafanaConf{}
	err = json.Unmarshal(bytes, u)
	if err != nil {
		fmt.Println("解析数据失败", err)
		return
	}
	fmt.Printf("%+v\n", u.Panels[0])
}


